package zero.tech.games.mj.work.sync;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSONObject;

import zero.tech.core.ZeroContext;
import zero.tech.core.data.KeyVal;
import zero.tech.core.util.StringHelper;
import zero.tech.core.work.AynWork;
import zero.tech.core.work.WorkManager;
import zero.tech.games.common.WaterMode;
import zero.tech.games.entity.games.Club;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.service.api.ILogicService;
import zero.tech.games.mj.data.MjConfig;
import zero.tech.games.mj.data.MjGameBase;
import zero.tech.games.mj.data.entity.PlayerData;
import zero.tech.games.mj.work.queue.AfterGameOverCalculateQueueWork;
import zero.tech.wsnet.controller.LogicController;

/**
 * 异步结算第一步
 * @author ludry
 *
 */
public class GameOverCalculateSyncWork extends AynWork{
	private Room room;
	private int bigcalsource;
	@Override
	public void init(Object... objs) throws Exception {
		room=(Room) objs[0];
		bigcalsource=(int) objs[1];
	}

	@Override
	public void run() {
		synchronized (room.getTransform_lock()) {
			bigCalculate(room);
			Club club=room.getClub();
			if(club!=null) {
				//结算代码在此处
				roomTransform(room);
			}
			WorkManager.getManager().submit(AfterGameOverCalculateQueueWork.class,room,bigcalsource);
		}
	}

	/**
	 * 房间分数结算
	 * @param room
	 */
	private void roomTransform(Room room) {
		MjGameBase gameBase = (MjGameBase) room.getGameBase();
		MjConfig mjConfig = gameBase.getMjConfig();
		int water_mode=mjConfig.getWater_mode();
		long water_num=mjConfig.getWater_num();
		long free_water_num=mjConfig.getFree_water_num();
		//玩家抽水数据
		Map<Integer,Long> watermap = new HashMap<>();
		//玩家输赢数据
		Map<Integer,Long> winmap = new HashMap<>();
		//玩家除去抽水后的输赢
		Map<Integer,Long> goldchangemap=new HashMap<>();
		//参与游戏玩家id
		List<Integer> uids=new ArrayList<Integer>();
		//参与游戏的玩家
		Map<Integer,LogicController> controllers = room.getAllControllers();
		//计算玩家输赢
		for (PlayerData player : gameBase.getPlayerMap().values()) {
			long win = (player.getWin() - player.getLose());
			winmap.put(player.getUid(), win);
			uids.add(player.getUid());
		}
		//计算抽水
		if(water_mode==WaterMode.WINER) {
			//大赢家抽水
			List<KeyVal<Integer, Long>>  list=StringHelper.parseMapToList(winmap);
			Collections.sort(list,new Comparator<KeyVal<Integer, Long>>() {
				@Override
				public int compare(KeyVal<Integer, Long> o1, KeyVal<Integer, Long> o2) {
					return (int) (o2.getV()-o1.getV());
				}
			});
			int bigwinner_uid=list.get(0).getK();
			long win=list.get(0).getV();
			if(win>free_water_num) {
				//超出面抽水限额,需要进行抽水
				watermap.put(bigwinner_uid, water_num);
			}
		}else {
			//AA抽水
			for(LogicController logicController : controllers.values()) {
				int uid=logicController.getId();
				watermap.put(uid, water_num);
			}
		}
		//除去抽水
		for(LogicController logicController : controllers.values()) {
			int uid = logicController.getId();
			long gold = logicController.getGold();
			long win = 0;
			if(winmap.containsKey(uid)) {
				win = winmap.get(uid);
			}
			if(watermap.containsKey(uid)) {
				win = win - watermap.get(uid);
			}
			gold = gold + win;
			logicController.setGold(gold);
			goldchangemap.put(uid, win);
		}
		//计算平分业绩
		if(water_mode == WaterMode.WINER) {
			long total=0;
			for(Entry<Integer,Long> entry : watermap.entrySet()) {
				total += entry.getValue();
			}
			watermap.clear();
			long pinjun = total / controllers.size();
			for(LogicController logicController : controllers.values()) {
				watermap.put(logicController.getId(), pinjun);
			}
		}
		String joiners = JSONObject.toJSONString(uids);
		ILogicService logicService = ZeroContext.getInstance(ILogicService.class);
		logicService.gameUpdateBatch(room, joiners, null, goldchangemap, watermap);
	}

	/**
	 * 房间数据统计
	 * @param room
	 */
	private void bigCalculate(Room room) {
		
	}
	
	
}
